package com.example.myapplication.Fragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myapplication.Adapter.OrderAdapter;
import com.example.myapplication.Adapter.commityApaer;
import com.example.myapplication.Bean.OrderBean;
import com.example.myapplication.Bean.communityBean;
import com.example.myapplication.R;
import com.example.myapplication.mySQL.mySQL;

import java.util.ArrayList;
import java.util.List;

public class OrderFragment extends Fragment {
    TextView text;
    ListView lv_order,lv_shuocang;
    List<OrderBean> list=new ArrayList<>();
    List<communityBean> list_shuocang=new ArrayList<>();
    OrderAdapter orderAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_order_fragment,null);
        lv_order=view.findViewById(R.id.lv_order);
        lv_shuocang=view.findViewById(R.id.lv_chuocang_order);
        text=view.findViewById(R.id.tv_title);
        text.setText("订单");
        //初始化数据
        list.add(new OrderBean(R.drawable.po1_hetao,"核桃","河北省临城县","30","元/公斤"));
        list.add(new OrderBean(R.drawable.po2_mi,"大米","黑龙江哈尔滨","50","元/公斤"));
        list.add(new OrderBean(R.drawable.po3_jidan,"鸡蛋","河北省内丘县","30","元/公斤"));
        list.add(new OrderBean(R.drawable.po4_huasheng,"花生","河北省承德市","15","元/公斤"));
        list.add(new OrderBean(R.drawable.po5_huajiao,"花椒","四川成都市","10","元/公斤"));
        //初始化适配器
        orderAdapter = new OrderAdapter(list,getActivity());
        lv_order.setAdapter(orderAdapter);
        //初始化sql数据
        mySQL mysqlHelper=new mySQL(getActivity(),"shoucang",null, 2);
        SQLiteDatabase db=mysqlHelper.getReadableDatabase();
        Log.i("1","1");
        //添加数据
        Cursor cursor=db.query("shoucang",null,null,null,null,null,null);
        Log.i("1","1");
        if (cursor!=null&&cursor.getCount()>0){
            //使游标回复到原点
            cursor.moveToFirst();
            for (int i=0;i<cursor.getCount();i++){
                //获取游标数据
                Log.i("1","1");
                int img=cursor.getInt(cursor.getColumnIndexOrThrow("img"));
                String biaoti=cursor.getString(cursor.getColumnIndexOrThrow("title"));
                String name=cursor.getString(cursor.getColumnIndexOrThrow("name"));
                String date=cursor.getString(cursor.getColumnIndexOrThrow("date"));
                String num=cursor.getString(cursor.getColumnIndexOrThrow("count"));
                Log.i("1","1");
                //创建OrderBean对象
                communityBean communityBean=new communityBean(img,biaoti,name,date,num);
                Log.i("1","1");
                //添加数据
                list_shuocang.add(communityBean);
                //移动游标
                cursor.moveToNext();

            }
            //关闭游标
            cursor.close();
        }
        //初始化适配器
        commityApaer commityApaer=new commityApaer(list_shuocang,getActivity(),1);
        //绑定
        lv_shuocang.setAdapter(commityApaer);
        return view;

    }
}